﻿namespace BEGDC.CC.Web.EZVisit
{
    #region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.Security;
    using System.Web.UI.WebControls;
    using ASBCC.Database.Entity;
    using ASBCC.Database.Service;
    using ASBCC.Workflow.Client;
    using Utility.Extensions;
    using Workflow;

    #endregion

    public partial class EZVRequestPrint : WorkflowBasePage, IEZVisitPageAccess
    {
        #region Properties

        private int AppId
        {
            get
            {
                int appId;

                int.TryParse(this.Request.QueryString["ID"], out appId);

                return appId;
            }
        }

        protected string SessionKey
        {
            get
            {
                if (string.IsNullOrEmpty(this.hidAppKey.Value))
                {
                    // Same session key for existing application, Different for new application
                    string key = this.AppId > 0 ? "EZVRequestApplication_" + this.AppId : DateTime.Now.Ticks.ToString();

                    this.hidAppKey.Value = FormsAuthentication.HashPasswordForStoringInConfigFile(key, "MD5");
                }

                return this.hidAppKey.Value;
            }
        }

        private EZVisitApplicationTableEntity Application
        {
            get
            {
                return this.Session[this.SessionKey] as EZVisitApplicationTableEntity;
            }
            set
            {
                this.Session[this.SessionKey] = value;
            }
        }

        private string CurrentUser
        {
            get
            {
                return Convert.ToString(this.ViewState["CurrentUser"]);
            }
            set
            {
                this.ViewState["CurrentUser"] = value;
            }
        }

        #endregion

        #region IEZVisitPageAccess Members

        public EZVisitAccessControl AccessControl { get; set; }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            this.AccessControl = new EZVisitAccessControl(this.UserInfo, this.AppId);

            if (!this.AccessControl.HasViewPermission)
            {
                this.Form1.Visible = false;
                this.lblFooter.Text = @"You were not granted the permission to view this page.<br/> <br/>您没有查看此页面的权限。";
                return;
            }

            this.CurrentUser = this.UserInfo.NameEN;

            this.Application = this.AccessControl.Application;

            if (this.Application == null)
            {
                this.Form1.Visible = false;
                this.lblFooter.Text = @"Invalid EZVisit application.<br/> <br/>无效申请。";
                return;
            }

            if (!this.Page.IsPostBack)
            {
                this.InitApplication();
                this.SetBranchAndAccountView();
                this.SetApplicantView();
                this.SetContractView();
                this.SetVisitPlacesView();
                this.SetCustomerView();
                this.SetAltAttendeeView();
                this.SetQuotAttchView();
                this.SetAftReturnView();
                this.SetAttchView();
            }
        }

        #region Workflow

        public override void RegisterWorkflowController()
        {
            this._workflowController = new EZVisitWorkflowController(this, this.GetAppForm(), this.GetMyActivity());
        }

        public override PlaceHolder GetPlaceHolder()
        {
            return this.phWorkflow;
        }

        public override void BuildFormArguments()
        {
            base.BuildFormArguments();
            Dictionary<string, object> formArguments = this.GetFormArguments();
            string TargetActivityName = this.GetCurrentTargetActivityName();

            formArguments[WorkflowKeyConsts.ModuleName] = "EZVisit Management";
            formArguments[EZVisitWorkflowKeyConsts.Applicant] = Convert.ToString(3637); //"di wan"3637
            formArguments[EZVisitActivityNameConsts.FNC_Controller] = this.GetDestination(EZVisitActivityNameConsts.FNC_Controller);
            formArguments[EZVisitActivityNameConsts.Dept_Head] = this.GetDestination(EZVisitActivityNameConsts.Dept_Head);

            int[] codppcp = this.GetDestination(EZVisitActivityNameConsts.COD_PP_Check_Person);
            if (codppcp.Length > 0)
            {
                formArguments[EZVisitWorkflowKeyConsts.CODPPCheckPerson] = codppcp[0];
            }
            int[] pdc = this.GetDestination(EZVisitActivityNameConsts.PDC);
            if (pdc.Length > 0)
            {
                formArguments[EZVisitWorkflowKeyConsts.PDC] = pdc[0];
            }

            formArguments[EZVisitActivityNameConsts.CEO_Delegation] = this.GetDestination(EZVisitActivityNameConsts.CEO_Delegation);
            formArguments[EZVisitActivityNameConsts.Post_Submission] = this.GetDestination(EZVisitActivityNameConsts.Post_Submission);
            formArguments[EZVisitActivityNameConsts.Post_COD_PP_Check_Person] = this.GetDestination(EZVisitActivityNameConsts.Post_COD_PP_Check_Person);
            formArguments[EZVisitActivityNameConsts.Post_Dept_Head] = this.GetDestination(EZVisitActivityNameConsts.Post_Dept_Head);
            formArguments[EZVisitActivityNameConsts.Post_Dept_Head] = this.GetDestination(EZVisitActivityNameConsts.Post_Dept_Head);
        }

        public override string GetProcName()
        {
            return "EZVisit";
        }

        public override IAppForm GetAppForm()
        {
            return new EZVisitAppForm(this.GetAppFormEntity());
        }

        private int[] GetDestination(string activityName)
        {
            var dt = new DataTable();
            var list = new List<int>();

            if (this.Application == null)
            {
                return new int[]
                {
                };
            }
            EmployeeTableEntity entity = EmployeeManager.SelectByKeys(this.Application.RequesterNo);

            switch (activityName)
            {
                case EZVisitActivityNameConsts.Dept_Head:
                case EZVisitActivityNameConsts.Post_Dept_Head:
                    if (entity != null)
                    {
                        list.Add(entity.ReportToID.Value);
                    }
                    break;
                case EZVisitActivityNameConsts.COD_PP_Check_Person:
                case EZVisitActivityNameConsts.Post_COD_PP_Check_Person:
                    if (this.Application.CODPPCheckPersonID.HasValue)
                    {
                        list.Add(this.Application.CODPPCheckPersonID.Value);
                    }
                    break;
                case EZVisitActivityNameConsts.PDC:
                    list = this.GetUserInfosByRole("PDC");
                    break;
                case EZVisitActivityNameConsts.CEO_Delegation:
                    if (this.Application.DepartmentName.HasValue)
                    {
                        DepartmentTableEntity department = DepartmentManager.SelectByKeys(this.Application.DepartmentName.Value);
                        if (department.CEODelegation != null)
                        {
                            EmployeeTableEntity ceoentity = EmployeeManager.SelectByKeys(department.CEODelegation);
                            list.Add(ceoentity.EmployeeID.Value);
                        }
                    }
                    break;
                case EZVisitActivityNameConsts.Post_Submission:
                    if (this.Application.RequesterID.HasValue)
                    {
                        list.Add(this.Application.RequesterID.Value);
                    }
                    break;
                default:
                    list.Add(1);
                    break;
            }
            return list.ToArray();
        }

        #endregion

        #region Init Application

        private void InitApplication()
        {
            if (this.Application.AppNo.IsNotNullOrWhiteSpace())
            {
                this.lbAppNo.Text = this.lblAppNo.Text = "&nbsp;&nbsp;Application No：" + this.Application.AppNo;
            }

            if (this.Application.FNCControllerDate != null)
            {
                this.lblReqNo.Text = "&nbsp;&nbsp;EZVisit No：" + this.Application.EZVisitReqNo;
                this.lbAppNo.Visible = false;
                this.lblAppNo.Visible = true;
            }
        }

        #endregion

        #region Branch&Account

        private void SetBranchAndAccountView()
        {
            this.lblCATBranch.Text = this.Application.CATBranch;

            this.lblAccount.Text = this.Application.Account;

            this.secAccount.Visible = this.Application.Account.IsNotNullOrWhiteSpace();
        }

        #endregion

        #region Applicant

        private void SetApplicantView()
        {
            if (!this.AccessControl.ApplicantTabView.Visible)
            {
                return;
            }

            this.pnlApplicant.Visible = this.ucApplicant.Visible = true;
            this.ucApplicant.SessionKey = this.SessionKey;
            this.ucApplicant.Editable = false;
            this.ucApplicant.CurrentUser = this.CurrentUser;

            if (!this.AccessControl.CommentTabView.Visible)
            {
                return;
            }

            this.ucComment.Visible = true;
            this.ucComment.SessionKey = this.SessionKey;
            this.ucComment.CurrentUser = this.CurrentUser;
        }

        #endregion Applicant

        #region Contract

        private void SetContractView()
        {
            this.pnlContract.Visible = this.ucContract.Visible = this.AccessControl.ContractTabView.Visible;
            this.ucContract.SessionKey = this.SessionKey;
            this.ucContract.Editable = false;
            this.ucContract.CurrentUser = this.CurrentUser;
        }

        #endregion

        #region Visit Places

        private void SetVisitPlacesView()
        {
            if (!this.AccessControl.ApplicationTabView.Visible)
            {
                return;
            }

            this.pnlVisit.Visible = this.ucVisit.Visible = true;
            this.ucVisit.SessionKey = this.SessionKey;
            this.ucVisit.Editable = false;
            this.ucVisit.CurrentUser = this.CurrentUser;
        }

        #endregion

        #region Customers

        private void SetCustomerView()
        {
            if (!this.AccessControl.ApplicationTabView.Visible)
            {
                return;
            }

            this.pnlCustomer.Visible = this.ucCustomer.Visible = true;
            this.ucCustomer.SessionKey = this.SessionKey;
            this.ucCustomer.Editable = false;
            this.ucCustomer.CurrentUser = this.CurrentUser;
        }

        #endregion

        #region Alt Attendees

        private void SetAltAttendeeView()
        {
            if (!this.AccessControl.ApplicationTabView.Visible)
            {
                return;
            }

            this.pnlAttendees.Visible = this.ucAltAttendee.Visible = true;
            this.ucAltAttendee.SessionKey = this.SessionKey;
            this.ucAltAttendee.Editable = false;
            this.ucAltAttendee.CurrentUser = this.CurrentUser;
        }

        #endregion

        #region Quotation Attachment

        private void SetQuotAttchView()
        {
            // Tab 7
            this.pnlQuotAttch.Visible = this.ucQuotAttch.Visible = this.AccessControl.QuotAttchTabView.Visible;
            this.ucQuotAttch.Editable = false;
            this.ucQuotAttch.CurrentUser = this.CurrentUser;
            this.ucQuotAttch.SessionKey = this.SessionKey;
        }

        #endregion

        #region AftReturn

        private void SetAftReturnView()
        {
            bool tabVisible = this.AccessControl.AftReturnTabsView.Visible;

            this.pnlFeedback.Visible = this.ucFeedback.Visible = tabVisible;
            this.pnlAftReturn.Visible = this.ucAftAttchment.Visible = tabVisible;
            this.ucFeedback.SessionKey = this.ucAftAttchment.SessionKey = this.SessionKey;
            this.ucFeedback.Editable = this.ucAftAttchment.Editable = false;
            this.ucFeedback.CurrentUser = this.ucAftAttchment.CurrentUser = this.CurrentUser;
        }

        #endregion

        #region Attchments

        private void SetAttchView()
        {
            if (!this.AccessControl.ApplicationTabView.Visible)
            {
                return;
            }

            this.pnlAttch.Visible = this.ucAttachment.Visible = true;
            this.ucAttachment.Editable = false;
            this.ucAttachment.SessionKey = this.SessionKey;
            this.ucAttachment.CurrentUser = this.CurrentUser;
        }

        #endregion

        #region Workflow Relational

        private EZVisitApplicationTableEntity _appFormEntity;

        private EZVisitApplicationTableEntity GetAppFormEntity()
        {
            this._appFormEntity = this.Application;
            if (this._appFormEntity == null)
            {
                EZVisitApplicationTableEntity entity;
                int? appFormId = this.GetAppFormId();
                if (appFormId.HasValue)
                {
                    entity = EZVisitApplicationManager.SelectByKeys(appFormId.Value);
                    if (entity == null)
                    {
                        throw new Exception(string.Format("Failed to Get EZVisit Application instance. ID={0}", appFormId));
                    }
                }
                else
                {
                    entity = new EZVisitApplicationTableEntity
                    {
                        RequesterID = this.UserInfo.EmployeeID,
                        RequesterNo = this.UserInfo.EmployeeNo,
                        RequesterNameEN = this.UserInfo.NameEN,
                        RequesterNameCN = this.UserInfo.NameChinese,
                        Folio = this.GetNewFolio()
                    };
                }
                this._appFormEntity = entity;
            }
            return this._appFormEntity;
        }

        #endregion
    }
}